#!/usr/bin/perl
# 2010/08/04 Automation program for Crop_FISHdata.pl

use strict;
use warnings;
use IO::File;
use Getopt::Std;
use Carp qw(croak);
$| = 0;

my $program = 'Crop_FISHdata.pl';

opendir DIR, "./" or die "cannot open directory: $!";
while(my $list = readdir DIR){
	unless(-d $list){
		next;
	}
	if($list =~ m/^\./){
		next;
	}
	opendir PROJ, $list or die "cannot open directory $list: $!";
	my $movie = '';
	my $green = '';
	my $red = '';
	my $output = $list . '.png';
	
	if(-e $output){
		print "$output is already exists. Skip this one\n";
		closedir PROJ;
		next;
	}
	
	while(my $file = readdir PROJ){
		if($file =~ m/\.avi$/){
			$movie = $file;
		}elsif($file =~ m/^green/i){
			$green = $file;
		}elsif($file =~ m/^red/i){
			$red = $file;
		}
	}
	
	if($movie eq '' or $green eq '' or $red eq ''){
		warn "$list has problems in there files\n";
	}else{ 
		my $command = "$program -i $list/$movie -g $list/$green -r $list/$red -o $output";
		my $out = qx/$command/;
	}
	closedir PROJ;
	
	print "$output was successfully created\n";
}
closedir DIR;


	
